<?php

class MenuController extends Controller {

    public function actionIndex() {
        $model = new Menu();
        if(isset($_POST['Menu'])){
            $model->attributes = $_POST['Menu'];
            $model->created = date('Y-m-d H:i:s');
            if($model->saveNode()){
                Yii::app()->user->setFlash('success', "menu đã được lưu");
                $model = new Menu();
            }
        }
        $menus = Menu::model()->roots()->findAll();
        $this->render('index',array('menus' => $menus,'model' => $model));
    }
    
    /**
     * 
     */
    public function actionDelete($id = null){
         if ($id != null ) {
            $model = Menu::model()->findByPk($id);
            if ($model != null) {
                if($model->deleteNode()){
                    Yii::app()->user->setFlash('success', "menu đã được xóa");
                    $this->redirect(array('menu/index'));
                }
            }else{
               throw new CHttpException(404, 'The specified post cannot be found.'); 
            }
         }else{
             throw new CHttpException(404, 'The specified post cannot be found.');
         }
    }
    
    /**
     * 
     */
    public function actionView($id = null){
        if ($id != null && $id != 1) {
            $model = Menu::model()->findByPk($id);
            if ($model != null) {
                $this->render('view',array('model' => $model));
            }else{
               throw new CHttpException(404, 'The specified post cannot be found.'); 
            }
         }else{
             throw new CHttpException(404, 'The specified post cannot be found.');
         }
    }
    
    /**
     * 
     */
    public function actionAddChild($parent_id = null){
        if($parent_id != null){
            $model = new Menu();
            $listMenu = Menu::getListMenu($parent_id);
            if (isset($_POST['Menu'])) {
                $model->attributes = $_POST['Menu'];
                $model->created = date('Y-m-d');
                $parent = Menu::model()->findByPk($_POST['Menu']['parents']);
                if ($parent != null) {
                    $image = CUploadedFile::getInstance($model, 'image');
                    if ($image != null) {
                        $newName = time() . "." . $image->extensionName;
                        $model->image = $newName;
                    }
                    $result = $model->appendTo($parent);
                    if ($result) {
                        if($image != null && isset($newName)){
                              $image->saveAs(PATH_IMAGE_PRODUCT . $newName);
                        }
                        Yii::app()->user->setFlash('success', "Danh mục sản phẩm đã được lưu");
                        $this->redirect(array('Menu/view', 'id' => $parent_id));
                    }
                }
            }
            $this->render('addChild', array('model' => $model, 'listMenu' => $listMenu));
        }else{
            throw new CHttpException(404, 'The specified post cannot be found.');
        }
    }
    
    /**
     * 
     */
    public function actionEditChild($parent_id = null,$id = null) {
        if ($id != null && $parent_id != null) {
            $model = Menu::model()->findByPk($id);
            if ($model != null) {
                if (isset($_POST['Menu'])) {
                    $parent = Menu::model()->findByPk($_POST['Menu']['parents']);
                    if ($parent != null) {
                        $model->attributes = $_POST['Menu'];
                        $image = CUploadedFile::getInstance($model, 'image');
                        if ($image != null) {
                            $newName = time() . "." . $image->extensionName;
                            $model->image = $newName;
                        }
                        if ($model->save() && $model->moveAsFirst($parent)) {
                            if($image != null && isset($newName)){
                                 $image->saveAs(PATH_IMAGE_Menu . $newName);
                            }
                            Yii::app()->user->setFlash('success', "Danh mục sản phẩm đã được lưu");
                            $this->redirect(array('Menu/view', 'id' => $parent_id));
                        }
                    }
                }
                $listMenu = Menu::getListMenu($parent_id, $id);
                $this->render('addChild', array('model' => $model, 'listMenu' => $listMenu, 'id' => $id));
            } else {
                throw new CHttpException(404, 'The specified post cannot be found.');
            }
        } else {
            throw new CHttpException(404, 'The specified post cannot be found.');
        }
    }
    
    /**
     * 
     */
    public function actionSearch(){
        $data = $this->searchProduct($_REQUEST['search']);
        if(isset($_REQUEST['type'])){
            switch ($_REQUEST['type']){
                case 2:
                  $data = $this->searchCategory($_REQUEST['search']);
                  break;  
                case 3:
                  $data = $this->searchArticle($_REQUEST['search']);
                  break; 
            }
        }
        echo json_encode($data);exit();
    }
    
    /**
     * 
     */
    protected function searchProduct($name){
        $return = array();
        $criteria = new CDbCriteria();
        $criteria->addCondition('status=1');
        $criteria->addCondition('name like :name');
        $criteria->params = array(':name' => '%'.$name.'%');
        $products = Product::model()->findAll($criteria);
        foreach($products as $item){
          $return[] = array(
              'id' => str_replace("/backend", "", $item->getUrl()),
              'name' => $item->name,
          ); 
        }
        return $return;
    }
    
    /**
     * 
     */
    protected function searchCategory($name){
        $return = array();
        $criteria = new CDbCriteria();
        $criteria->addCondition('name like :name');
        $criteria->params = array(':name' => '%'.$name.'%');
        $products = Category::model()->findAll($criteria);
        foreach($products as $item){
          $return[] = array(
              'id' => str_replace("/backend", "", $item->getUrl()),
              'name' => $item->name,
          ); 
        }
        return $return;
    }
    
    /**
     * 
     */
    protected function searchArticle($name){
        $return = array();
        $criteria = new CDbCriteria();
        $criteria->addCondition('title like :name');
        $criteria->params = array(':name' => '%'.$name.'%');
        $products = News::model()->findAll($criteria);
        foreach($products as $item){
          $return[] = array(
              'id' => str_replace("/backend", "", $item->getUrl()),
              'name' => $item->name,
          ); 
        }
        return $return;
    }
    
    /**
     * 
     */
    public function actionDeleteChild($parent_id = null,$id = null){
         if ($id != null && $parent_id != null) {
            $model = Menu::model()->findByPk($id);
            if ($model != null) {
                if($model->deleteNode()){
                    Yii::app()->user->setFlash('success', "menu đã được xóa");
                    $this->redirect(array('menu/view','id' => $parent_id));
                }
            }else{
               throw new CHttpException(404, 'The specified post cannot be found.'); 
            }
         }else{
             throw new CHttpException(404, 'The specified post cannot be found.');
         }
    }

}